package com.qf.zyh.services.Impl;

import com.qf.zyh.common.Const;
import com.qf.zyh.dao.Impl.UserDaoImpl;
import com.qf.zyh.dao.UserDao;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.UserServices;
import com.qf.zyh.util.*;
import org.apache.commons.dbutils.DbUtils;

import java.util.List;

public class UerServicesImpl implements UserServices {
    UserDao userDao = new UserDaoImpl();
    @Override
    public List<User> selectAll() {
        try {
            DBUtil.begin();
            List<User> users = userDao.selectAll();
            DBUtil.commit();
            return users;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    @Override
    public User selectName(String name) {
        try {
            DBUtil.begin();
            if (StrUtils.empty(name)) {
                User user = userDao.selectName(name);
                DBUtil.commit();
                if (user!=null) {
                    return user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    @Override
    public boolean addUser(User user) {
        boolean fiag = false;
        if (user!=null) {
        try {
            DBUtil.begin();
                int i = userDao.addUser(user);
                DBUtil.commit();
                if(i>0){
                    /*new Thread(new Runnable() {
                        @Override
                        public void run() {
                            EmailUtils.sendEmail(user);
                        }
                    }).start();匿名内部类用lambda表达式*/
                    new Thread(() -> EmailUtils.sendEmail(user)).start();
                    fiag=true;
                }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        }
        return fiag;
    }

    @Override
    public String activate(String c, String e) {
        try {
            DBUtil.begin();
            //三种情况，一是用户不存在，二用户是不是激活过了，没激活就激活反馈激活成功。decode是解码
            User us = userDao.getUserByEmailAndCode(Base64Utils.decode(c),Base64Utils.decode(e));
            DBUtil.commit();
            if (us==null) {
                return "没有该用户";
            }
            if (us.getUstatus()==Const.UserCont.ACTIVATE) {
                return "已激活过";
            }
            int i = userDao.updateUserStatus(Const.UserCont.ACTIVATE,us.getUid());
            if (i>0) {
                return "激活成功";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            DBUtil.rollback();
        }
        return "激活失败";
    }

    @Override
    public User login(String username, String password) {
        User use = null;
        try {
            DBUtil.begin();
            if (StrUtils.empty(username,password)) {
                User user = userDao.selectName(username);
                DBUtil.commit();
                //验证返回的是否为空，并且user中的密码为加密密码，所以要将传入密码加密，才能进行比较。
                if (user!=null&&user.getUpassword().equals(MD5Utils.md5(password))){
                    use = user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return use;
    }
}
